package com.lvmama.config;

import com.lvmama.common.utils.EncodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * Security权限核心模块
 *
 * @author caoyong
 * @since 2018年1月29日 下午12:48:30
 */
@Slf4j
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private CustomizeUserSecurityConfig customizeUserSecurityConfig;
    @Autowired
    private CustomSuccessHandler customSuccessHandler;

    /**
     * 注册UserDetailsService 的bean
     *
     * @return 自定义userService
     */
    @Bean
    UserDetailsService customUserService() {
        log.info("注册自定义userService:{}", customizeUserSecurityConfig.toString());
        return customizeUserSecurityConfig;
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        log.info("使用自定义customizeUserSecurityConfig:{}", customizeUserSecurityConfig);
        //user Details Service验证
        auth.userDetailsService(customizeUserSecurityConfig).passwordEncoder(new PasswordEncoder() {
            @Override
            public String encode(CharSequence rawPassword) {
                return EncodeUtil.encode((String) rawPassword);
            }

            @Override
            public boolean matches(CharSequence rawPassword, String encodedPassword) {
                return encodedPassword.equals(EncodeUtil.encode((String) rawPassword));
            }
        });

    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //关闭打开的csrf保护
        http.csrf().disable();
        http.headers().disable()
                .authorizeRequests()
                    // 针对spring boot admin的url,否则spring boot admin无法获取服务信息
                    .antMatchers("/actuator/**").permitAll()
                    .anyRequest().authenticated()
                    .and()
                .formLogin()
                    .loginPage("/login").successHandler(customSuccessHandler).permitAll()
                    .and()
                .headers().frameOptions().disable()
                    .and()
                .logout()
                    .logoutUrl("/logout").permitAll().logoutSuccessUrl("/login");
        log.info("http规则配置");
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/static/**");
    }
}
